#!/usr/bin/python

#    This file is part of meterserv - a simple utility to log and display
#    readings from utility meters (see http://meterserv.webhop.net)
#
#    Meterserv is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Townguide is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with townguide.  If not, see <http://www.gnu.org/licenses/>.
#
#    Copyright Graham Jones 2011
#
# Based on the excellent webpy tutorial at 
# http://kooneiform.wordpress.com/2010/02/28/python-and-ajax-for-beginners-with-webpy-and-jquery/
# Sqlite bits taken from http://kzar.co.uk/blog/view/web.py-tutorial-sqlite



import web, datetime

db = web.database(dbn='sqlite', db='meters.db')

def get_meters():
    return db.select('meters', order='id ASC')

def get_meter(id):
    try:
        return db.select('meters', where='id=$id', vars=locals())[0]
    except IndexError:
        return None

def new_meter(title):
    db.insert('meters')

def del_meter(id):
    db.delete('meters', where="id=$id", vars=locals())

def update_meter(id,description,unit,cfac,output_period,\
                     iocard,inputno,reading):
    print "update_meter"
    db.update('meters', where="id=$id", vars=locals(),
              description=description,
              unit=unit,
              cfac=cfac,
              output_period=output_period,
              iocard=iocard,
              input=inputno,
              reading=reading)

def get_readings(id):
    try:
        web.debug("model.get_readings")
        readings =  db.select('readings', where='meter=$id', vars=locals())
        #web.debug(readings)
        return readings
    except IndexError:
        web.debug("get_readings - IndexError")
        return None

def get_dates(id):
    """
    Returns a list containing the minimum and maximum date for
    readings in the database for meter number id.
    """
    try:
        web.debug("model.get_dates")
        maxmindate =  db.query('select max(time) as maxTime,min(time) as minTime from readings where meter=$id', vars=locals())
        retIter = maxmindate[0]
        web.debug(retIter)
        retval = 'revaval'
        #retval = (maxmindate[0]['minTime'],maxmindate[0]['maxTime']) 
        retval = (retIter['minTime'], retIter['maxTime'] )
        return retval
    except IndexError:
        web.debug("get_dates - IndexError")
        return "model.get_dates: Error - Sorry"

def query_db(sqlStr):
    """
    Returns the results of a given SQL query on the database.
    """
    try:
        web.debug("model.query_db('%s')" % sqlStr)
        results =  db.query(sqlStr)
        return results
    except IndexError:
        web.debug("query_db - IndexError")
        return "model.query_db: Error - Sorry"

